import time
from pyo import *
from pref import prefs
# from main import modulesDict

class AudioServer:
    """
    audioserver class contains all fx, sound player/recorder
        saves and recalls state 
        access to osc and audio settings (init dialogue for setup)
    """
    def __init__(self):
        # print modulesDict
        self.s = Server(nchnls = prefs['channels'], duplex = 1)
        print "output driver n ", prefs["audioDriver"]
        print " input driver n ", prefs["audioDriverInput"]
        self.s.setNchnls(prefs['channels'])
        self.s.setOutputDevice(prefs["audioDriver"])
        self.s.setInputDevice(prefs["audioDriverInput"])
        # print "yeo device"
        time.sleep(0.1)
        self.s.boot()

        import OscServer
        OscServer.addCtrlr("lemur1", 8000, 7999,8, prefs["IpadIP"])#"169.254.126.129"
        from NewOscClass import *
        from AudioInStrip import *
        self.s.start()
        self.osc = OscServer.OscCtrls()
        #\\ global init



        self.modOuts = {0:{"name": "Empty","audio": Sig(0)},
                1:{"name": "Mod1","audio": Sig(0)},
                2:{"name": "Mod2","audio": Sig(0)},
                3:{"name": "Mod3","audio": Sig(0)}}

        self.fxOscCtrl = [OscClass("/SlotFx%d" % (i+1), self.modOuts,"fx") for i in range(3)]
        self.modOscCtrl = [OscClass("/SlotMod%d" % (i+1), self.modOuts,"mod") for i in range(3)]

        self.inst = AudioInStrip( audioIn = Noise([0.1 for i in range(prefs["channels"])]))
        # // audio routing
        self.fxOscCtrl[0].setAudio(self.inst.getAudio())
        for i in range(2):
            self.fxOscCtrl[i+1].setAudio(self.fxOscCtrl[i].getAudio())
        self.fxOscCtrl[2].getAudio().out()

        # //mod routing
        for i in range(3):
            self.modOuts[i+1]["audio"] = self.modOscCtrl[i].getMod() 
        





# s.setVerbosity(15)


def q():
    s.stop()
    time.sleep(1)
    exit()